/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

var isShowingMsg = false;
var isLogin = false;
var username;

var winLogin ;
function test()
{
    alert(document.getElementById('test').innerHTML);
}
function showLoginDialog(imgId,serverPath)
{
    if(winLogin==null)
    {
        winLogin = new Ext.Window({
            el:'divLoginWindow',
            //layout:'fit',
            //modal:true,
            title:'<center>用户登录</center>',
            width:250,
            height:192,
            resizable:false,
            closeAction:'hide',
            items: [divLoginInfo],
            buttons: [{
                text:'确  定',
                handler: function(){login(imgId,serverPath);}//function(){document.getElementById('loginSubmit').click();}
            }]
        });
    }
    winLogin.show(Ext.get('loginBtn').dom);//
   // if(winReg!=null)
        //winReg.hide();
    getVerifyCode('verifyCodeImageLogin',serverPath);
    //document.getElementById('divLoginInfo').getElementsByTagName('input')[0].focus();
}
function login(imgId,serverPath)
{
    var isConnectSuccess = false;
    var xxx = Ext.MessageBox.show({
        id:'loginProcess',
        title: '请稍等',
        msg: '正在登录...',
        progressText: '',
        width:300,
        progress:true,
        closable:true,
        animEl: 'loading'
    });
    //控制进度速度
    var f = function(v){
        return function(){
            var i = v/20;
            Ext.MessageBox.updateProgress(i, '正在登录');
            if(v==20&&!isConnectSuccess)
            {
                //Ext.Msg.alert('提示','对不起，连接服务器超时，请重新尝试');
                winLogin.hide();
                //isShowingMsg = false;//重新设置消息提示框的状态为假
            }
        };
    };

    for(var i = 1; i < 21; i++){
        setTimeout(f(i), i*300);
    }
    var conn = new Ext.data.Connection();
    username =  document.getElementById('divLoginInfo').getElementsByTagName('input')[0].value;
      // 发送异步请求
    conn.request({
      // 请求地址
        url: 'login.action',
        method: 'GET',
        params:'userName='+username+'&password='+document.getElementById('divLoginInfo').getElementsByTagName('input')[1].value+'&verifyCode='+document.getElementById('divLoginInfo').getElementsByTagName('input')[2].value,
        // 指定回调函数
        callback: callback
    });
    //回调函数
    function callback(options, success, response){
        if(success){
          // 如果成功则使用Ext将JSON字符串转换为JavaScript对象
            var jsonObj = Ext.util.JSON.decode(response.responseText);
            if(jsonObj.answer== 1)
            {
                isConnectSuccess = true;
                winLogin.hide();
                xxx.hide();
                isLogin = true;
                var inputBtn = document.getElementById("loginBtn");
                inputBtn.style.display="none";
                var showUser = document.getElementById("showUser");
                showUser.innerHTML = "Welcome, " + username;               
                TipMessageShow(jsonObj.title,jsonObj.msg,divMessageTipLogin,'divMessageTipLogin');
            }
            else
            {
                isConnectSuccess = 2;
                Ext.Msg.alert(jsonObj.title,jsonObj.msg);
                getVerifyCode(imgId,serverPath);
            }
        }
        else
        {
            Ext.Msg.alert('错误提示','连接服务器失败，请重试');
            getVerifyCode(imgId,serverPath);
        }
    }
}
function getVerifyCode(imgId,serverPath)
{
    document.getElementById(imgId).src=serverPath + "tools/verifyCode.jsp?width=80&heigth=24&temp=" + new Date().getTime().toString(26);
    return false;
}

var winRegister ;
function showRegisterDialog(imgId,serverPath)
{
    if(winRegister==null)
    {
        winRegister = new Ext.Window({
            el:'divRegWindow',
            //modal:true,
            title:'<center>用户注册</center>',
            width:260,
            height:202,
            resizable:false,
            closeAction:'hide',
            items: [divRegInfo],
            buttons: [{
                text:'确  定',
                handler: function(){login(imgId);}//function(){document.getElementById('loginSubmit').click();}
            }]
        });
    }
    winRegister.show(Ext.get('registerBtn').dom);//
   // if(winReg!=null)
        //winReg.hide();
    getVerifyCode('verifyCodeImageRegister',serverPath);
    //document.getElementById('divLoginInfo').getElementsByTagName('input')[0].focus();
}


function checkUser(userName,textId)
{
    var conn = new Ext.data.Connection();
      // 发送异步请求
    conn.request({
      // 请求地址
        url: 'checkUser.action',
        method: 'GET',
        params:'userName='+userName,
        // 指定回调函数
        callback: callback
    });
    //回调函数
    function callback(options, success, response){
        if(success){
          // 如果成功则使用Ext将JSON字符串转换为JavaScript对象
            var jsonObj = Ext.util.JSON.decode(response.responseText);
            if(jsonObj.answer == 1)
            {
                document.getElementById(textId).color = '#ff0000';
                con_username=false;setSubmit();
            }
            else
            {
                document.getElementById(textId).color = '#000000';
                con_username=true;setSubmit();
            }
            document.getElementById(textId).innerHTML = jsonObj.msg;
        }
        else
        {
            Ext.Msg.alert('错误提示','连接服务器失败');
        }
    }
}

